package com.city.parking;

import java.util.ArrayList;
import java.util.List;
import com.city.parking.map.OfflineMapSettings;
import com.city.parking.util.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SlideFragment extends Fragment implements OnClickListener {
	private Activity activity;
	private TextView mPhoneTV, mCarIdTV, mLoginTV;
	private LinearLayout mUserInfoLL;
	private ImageView mIcon;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState) {
		initSideMenuList();
		View view = inflater.inflate(R.layout.side_menu, container, false);
		ListView listView = (ListView) view.findViewById(R.id.side_menu_list);
		SideMenuAdapter sideMenuAdapter = new SideMenuAdapter(
		        this.getActivity());
		listView.setAdapter(sideMenuAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
			        long arg3) {
				// TODO Auto-generated method stub

				SideMenuItem item = mItemList.get(pos);
				Intent i = null;
				switch (item.id) {
				case MENU_ITEM_ORDER:
                    i = new Intent(activity, OrderListActivity.class);
                    startActivity(i);
                    break;
				case MENU_ITEM_ACCOUNT:
					i = new Intent(activity, ExtensionActivity.class);
					i.putExtra("icon", item.icon);
					i.putExtra("title", item.title);
					startActivity(i);
					break;
				case MENU_ITEM_OFFLINE_MAP:
					i = new Intent(activity, OfflineMapSettings.class);
					startActivity(i);
					break;
				case MENU_ITEM_CARS:
                    i = new Intent(activity, MyCarsActivity.class);
                    startActivity(i);
                    break;
				case MENU_ITEM_LEASE:
                    i = new Intent(activity, LeaseListActivity.class);
                    startActivity(i);
                    break;
				case MENU_ITEM_VIOLATION:
					i = new Intent(activity, TrafficViolationListActivity.class);
					startActivity(i);
					break;
				case MENU_ITEM_MOVE_CAR:
					i = new Intent(activity, MoveCarActivity.class);
					startActivity(i);
					break;
				case MENU_ITEM_SETTING:
                    i = new Intent(activity, SettingActivity.class);
                    startActivity(i);
                    break;
				}
			}

		});
		activity = this.getActivity();

		mPhoneTV = (TextView) view.findViewById(R.id.phone);
		mCarIdTV = (TextView) view.findViewById(R.id.car_id);
		mLoginTV = (TextView) view.findViewById(R.id.login);
		mUserInfoLL = (LinearLayout) view.findViewById(R.id.user_info_ll);
		mIcon = (ImageView) view.findViewById(R.id.user_icon);
		mIcon.setOnClickListener(this);

		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Utils.log("Slide Frame onResume");
		if (ParkingApp.mUid < 0) {
			mUserInfoLL.setVisibility(View.GONE);
			mLoginTV.setVisibility(View.VISIBLE);
			Utils.log("Slide Frame gone");
		} else {
			mPhoneTV.setText(ParkingApp.mPhone);
			mCarIdTV.setText(ParkingApp.mCarId);
			mUserInfoLL.setVisibility(View.VISIBLE);
			mLoginTV.setVisibility(View.GONE);
			Utils.log("Slide Frame show");
		}
	}

	private static List<SideMenuItem> mItemList = null;
	private UserRegister mRegister = null;
	private final int MENU_ITEM_ORDER = 1;
	private final int MENU_ITEM_ACCOUNT = 2;
	private final int MENU_ITEM_OFFLINE_MAP = 3;
	private final int MENU_ITEM_CARS = 5;
	private final int MENU_ITEM_LEASE = 6;
	private final int MENU_ITEM_VIOLATION = 7;
	private final int MENU_ITEM_SETTING = 8;
	private final int MENU_ITEM_MOVE_CAR = 9;

	private void initSideMenuList() {
		if (mItemList == null) {
			mItemList = new ArrayList<SideMenuItem>();
			mItemList.add(new SideMenuItem("我的订单",
			        R.drawable.side_order_selector, MENU_ITEM_ORDER));
			mItemList.add(new SideMenuItem("我的账户",
			        R.drawable.side_wallet_selector, MENU_ITEM_ACCOUNT));
			mItemList.add(new SideMenuItem("离线地图", 
					R.drawable.side_map_selector, MENU_ITEM_OFFLINE_MAP));
			mItemList.add(new SideMenuItem("我的爱车",
					R.drawable.side_car_selector, MENU_ITEM_CARS));
			mItemList.add(new SideMenuItem("车位租赁",
					R.drawable.side_lease_selector, MENU_ITEM_LEASE));
			mItemList.add(new SideMenuItem("我的违章",
					R.drawable.side_car_selector, MENU_ITEM_SETTING));
			mItemList.add(new SideMenuItem("我要移车",
					R.drawable.side_car_selector, MENU_ITEM_MOVE_CAR));
			mItemList.add(new SideMenuItem("设置",
					R.drawable.side_setting_selector, MENU_ITEM_SETTING));
		}
	}

	private class ViewHolder {
		TextView title;
		ImageView icon;
	}

	private class SideMenuItem {
		public String title;
		public int icon;
		public int id;

		public SideMenuItem(String name, int res, int i) {
			title = name;
			icon = res;
			id = i;
		}
	}

	private class SideMenuAdapter extends BaseAdapter {
		private LayoutInflater inflater = null;

		public SideMenuAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return mItemList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.side_menu_item, null);
				holder.title = (TextView) convertView
				        .findViewById(R.id.item_title);
				holder.icon = (ImageView) convertView
				        .findViewById(R.id.item_icon);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			ImageView divider = (ImageView) convertView
			        .findViewById(R.id.item_divider);
			if (position == mItemList.size() - 1)
				divider.setVisibility(View.VISIBLE);
			else
				divider.setVisibility(View.GONE);

			SideMenuItem item = mItemList.get(position);
			holder.icon.setImageResource(item.icon);
			holder.title.setText(item.title);
			return convertView;
		}
	}

	@Override
	public void onDestroy() {
		if (mRegister != null) {
			mRegister.onDestroy();
		}
		super.onDestroy();
	}
	
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
    		if (msg.what == UserRegister.MSG_USER_REGISTER) {
    			if (msg.arg1 == UserRegister.USER_REGISTER_SUCCESS) {
    				if (ParkingApp.mUid >= 0) {
    					mPhoneTV.setText(ParkingApp.mPhone);
    					mCarIdTV.setText(ParkingApp.mCarId);
    					mUserInfoLL.setVisibility(View.VISIBLE);
    					mLoginTV.setVisibility(View.GONE);
    					Utils.log("Slide Frame show");
    				}
    			}
    		}
        }
    };

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.user_icon:
			if (ParkingApp.mUid < 0) {
				mRegister = new UserRegister(activity, mHandler);
				mRegister.launchRegisterPage();
			}
			break;
		}
	}
}
